package com.yonyougov.devops.cruxcisupport.harbor.project;

import com.yonyougov.devops.cruxcisupport.harbor.utils.HarborInfo;
import com.yonyougov.devops.cruxcisupport.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class HarborProjectDao {
//    @Value("${harbor.username}")
//    private String harborUserName;
//    @Value("${harbor.password}")
//    private String harborPassWord;
//    @Value("${harbor.url}")
//    private String harborUrl;
    @Autowired
    private HarborInfo harborInfo;

    public List<HarborProject> getProjects() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setBasicAuth(harborInfo.getUsername(),harborInfo.getPassword());
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        String url = harborInfo.getUrl() + "projects";
        ResponseEntity<List<HarborProject>> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, new ParameterizedTypeReference<List<HarborProject>>() {
        });
        if(responseEntity.getStatusCode().value() == 200 && responseEntity.hasBody()){
            return responseEntity.getBody();
        }
        return null;
    }
}
